import cPickle

def add_to_pickled_dict(key, value, pickle_filename):
    '''
    Add the key value pair to a pickled dictionary.  The dictionary will be read in from a pickle
        file, and the key, value pair will be added.  The updated dictionary will be pickled back
        to the pickle file.
    '''
    # open the pickle file or start a new dictionary.
    try:
        pickle_file = open(pickle_filename,'r')
        no_previous_pickle_file = False
    except:
        no_previous_pickle_file = True 
        
    if no_previous_pickle_file:
        d = {}
    else:
        d = cPickle.load(open(pickle_filename))

    # add the key value pair to the dictionary.
    d[key] = value
    # dump the updated dictionary back to the pickle file.
    cPickle.dump(d, open(pickle_filename,'w'), protocol=-1) # use most efficient method possible


